package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.SmsTemplateForm;
import com.dimples.dd.system.model.query.SmsTemplatePageQuery;
import com.dimples.dd.system.model.vo.SmsTemplatePageVO;
import com.dimples.dd.system.model.vo.SmsTemplateSendReqVO;
import com.dimples.dd.system.service.SmsSendService;
import com.dimples.dd.system.service.SystemSmsTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.simpleframework.xml.core.Validate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/10/28
 */
@Validate
@Tag(name = "短信管理-短信模板")
@RestController
@RequestMapping("sms-template")
@RequiredArgsConstructor
public class SysSmsTemplateController {

    private final SystemSmsTemplateService systemSmsTemplateService;
    private final SmsSendService smsSendService;

    @PostMapping
    @Operation(summary = "创建短信模板")
    @PreAuthorize("@ss.hasPerm('system:sms-template:create')")
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Long> createSmsTemplate(@Valid @RequestBody SmsTemplateForm createSmsTemplateForm) {
        return CommonResult.success(this.systemSmsTemplateService.createSmsTemplate(createSmsTemplateForm));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新短信模板")
    @PreAuthorize("@ss.hasPerm('system:sms-template:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateSmsTemplate(
            @PathVariable Long id,
            @Valid @RequestBody SmsTemplateForm updateSmsTemplateForm
    ) {
        updateSmsTemplateForm.setId(id);
        this.systemSmsTemplateService.updateSmsTemplate(updateSmsTemplateForm);
        return CommonResult.success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除短信模板")
    @PreAuthorize("@ss.hasPerm('system:sms-template:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteSmsTemplate(
            @Parameter(name = "id", description = "编号", required = true) @PathVariable String id
    ) {
        this.systemSmsTemplateService.deleteSmsTemplate(id);
        return CommonResult.success(true);
    }

    @GetMapping("/{id}/form")
    @Operation(summary = "短信模板表单数据")
    @PreAuthorize("@ss.hasPerm('system:sms-template:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<SmsTemplateForm> selectSmsTemplate(
            @Parameter(name = "id", description = "编号", required = true, example = "1024") @PathVariable Long id
    ) {
        SmsTemplateForm channel = this.systemSmsTemplateService.selectSmsTemplate(id);
        return CommonResult.success(channel);
    }

    @GetMapping("/page")
    @Operation(summary = "获得短信模板分页")
    @PreAuthorize("@ss.hasPerm('system:sms-template:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public PageResult<SmsTemplatePageVO> selectSmsTemplatePage(@Valid SmsTemplatePageQuery pageQuery) {
        return this.systemSmsTemplateService.selectSmsTemplatePage(pageQuery);
    }

    @PostMapping("/send-sms")
    @Operation(summary = "发送短信")
    @PreAuthorize("@ss.hasPerm('system:sms-template:send-sms')")
    public CommonResult<Long> sendSms(@Valid @RequestBody SmsTemplateSendReqVO sendReqVO) {
        return CommonResult.success(this.smsSendService.sendSingleSmsToAdmin(sendReqVO.getMobile(), null, sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
    }

}










